package com.zpf.controller;

import com.google.common.collect.Lists;
import com.zpf.entity.User;
import com.zpf.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: SimpleProviderUserController
 * @Description: TODO
 * @Autor: ZPF
 * @Date: 2021/1/13 10:53
 **/
@RestController
public class ProviderUserController {

    @Autowired
    private UserRepository userRepository;

//    @Autowired
//    private EurekaClient discoveryClient;

    @GetMapping("/simple/{id}")
    public User findById(@PathVariable Long id){
        return userRepository.findOne(id);
    }

    @GetMapping("/simple")
    public List<User> findAll(){
        return userRepository.findAll();
    }

//    public String serviceUrl(){
//        InstanceInfo instance = this.discoveryClient.getNextServerFromEureka("PROVIDER-USER", false);
//        return instance.getHomePageUrl();
//    }

    @PostMapping("/user")
    public User postUser(@RequestBody User user){
        System.out.println("@GetMapping(\"user\") 接收参数对象 user: " + user);
        return user;
    }

    @GetMapping("listAll")
    public List<User> listAll(){
        ArrayList<User> list = Lists.newArrayList();
        for (int i = 0; i < 5; i++) {
            int index = i+1;
            User user = new User((long)index, "user"+index, "", Short.valueOf(index+10+""), new BigDecimal(index*1000));
            list.add(user);
        }

        return list;
    }

}
